// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Common from "../Common";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Profiles extends Common {

    @property(cc.Node)
    logoutBtn: cc.Node;

    @property(cc.Node)
    backBtn: cc.Node;

    @property(cc.Node)
    rightBtn: cc.Node;

    @property(cc.Label)
    userNameLabel: cc.Label

    @property(cc.Label)
    amountLabel: cc.Label

    @property(cc.Label)
    levelNameLabel: cc.Label

    @property(cc.Node)
    content: cc.Node;

    protected watch = {
        userInfo: (val) => {
            const account = val.account || {}
            this.userNameLabel.string = val.user_id || ''
            this.levelNameLabel.string = val.invest_level_name || ''
            const amount = account.cash_account || '0.00'
            this.amountLabel.string = `余额：${amount}`
        }
    }

    start() {
        this.setKaiQiFrame(this.getAudioManager().getIsMute())
        this.logoutBtn.on(cc.Node.EventType.TOUCH_START, this.onLogout, this)
        this.backBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            cc.director.loadScene('hall')
        }, this)
        this.rightBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            const isMute = !this.getAudioManager().getIsMute()
            cc.sys.localStorage.setItem('isMute', isMute ? 'true' : '')
            this.getAudioManager().setIsMute(isMute)
            this.setKaiQiFrame(isMute)
        }, this)
        this.content.children.forEach((item, rowIndex) => {
            item.children.forEach((col, colIndex) => {
                col.on(cc.Node.EventType.TOUCH_START, this.onItemClick.bind(this, rowIndex, colIndex), this)
            })

        })
    }

    protected setKaiQiFrame(isMute = false) {
        const path = isMute ? 'kaiqi_1' : 'kaiqi'
        cc.resources.load(path, cc.SpriteFrame, (err, item: cc.SpriteFrame) => {
            if (!err) {
                this.rightBtn.getComponent(cc.Sprite).spriteFrame = item
            }

        })
    }

    private onItemClick(rowIndex, colIndex) {
        let runSource = true
        if (rowIndex === 0) { // 第一行
            switch (colIndex) {
                case 0: // 提现申请
                    this.node.getChildByName('withdrawDialog').active = true
                    break;
                case 1: // 提现记录
                    cc.director.loadScene('withdrawRecords')
                    break;
                case 2: // 充值申请
                    this.node.getChildByName('rechargeDialog').active = true
                    break;
                case 3: // 充值记录
                    cc.director.loadScene('rechargeRecords')
                    break;
                default:
                    runSource = false
            }
        } else if (rowIndex === 1) { // 第二行
            switch (colIndex) {
                case 0: // 我的团队
                    cc.director.loadScene('users')
                    break;
                case 1: // 超级树苗
                    cc.director.loadScene('userLevel')
                    break;
                case 2: // 奖金记录
                    cc.director.loadScene('bonus')
                    break;
                case 3: // 树苗商城
                    cc.director.loadScene('shop')
                    break;
                default:
                    runSource = false
            }
        }

        if (runSource) {
            this.onBtnClick()
        }
    }

    async onLogout() {
        try {
            this.onBtnClick()
            const {message} = await this.http.get('auth/logout')
            this.showToast(message)
            this._data.userInfo = {}
            this._data.hasToken = false
            this._data.currentScene = 'login'
            cc.sys.localStorage.setItem('userToken', '')
        } catch ({message}) {
            this.showToast(message)
        }
    }
}
